package org.jkuang.qstar.commons.jni;

import java.io.File;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

public  class RMDB extends Native {
	int module = integer.getAndIncrement();
	static AtomicInteger integer = new AtomicInteger(0);

	public RMDB(boolean compress, boolean ignoreCase,int node_age_length) {
		create(module, ignoreCase, compress,node_age_length);
	}

	
	
	public void destroy() {
		destroy(module);
	}

	public void insert(long key, long sort, Collection<String> terms, byte[] data) {
		if (terms == null || terms.size() == 0) {
			return;
		} else {
			addDoc(module, key, sort, terms.toArray(new String[0]), data);
		}
	}

	public byte[] query(String syntax, long _s_sort, long _e_sort, int start, int rows, int count, boolean desc) {
		return query(module, syntax, _s_sort, _e_sort, start, rows, count, desc);
	}

	public void delete(long key) {
		delDoc(module, key);
	}

	public   int load(String fileName) {
		return this.load(module,fileName);
	}

	public   int dump(String fileName) {
		File file = new File(fileName);
		File dir = file.getParentFile();
		if(!dir.exists()) {
			dir.mkdirs();
		}
		return this.dump(module, fileName);
	}
	
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		destroy();
	}

	private  native int create(int index, boolean ignoreCase, boolean compress,int node_age_length);

	private  native int destroy(int index);

	private  native int load(int index, String fileName);

	private  native int dump(int index, String fileName);

	private  native int addDoc(int index, long key, long sort, String[] terms, byte[] data);

	private  native int delDoc(int index, long key);

	// long key1,long sort1,byte[] data1,long key2,long sort2,byte[] data2,
	private  native byte[] queryByKeys(int index, long[] keys);

	// long key1,long sort1,byte[] data1,long key2,long sort2,byte[] data2,
	private  native byte[] query(int index, String syntax, long _s_sort, long _e_sort, int start, int rows,	int count, boolean desc);
}
